﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FloodFree.Web.Mailers;
using FloodFree.Common;
using Mvc.Mailer;
using System.Net.Mail;

namespace FloodFree.Web.Mailers
{
    public class Notifier
    {
        public void Notify(int sensorId, int statusType, int status)
        {
            NotifyByEmail(sensorId, statusType, status);
            NotifyByPhoneMessage(sensorId, statusType, status);
        }

        private void NotifyByEmail(int sensorId, int statusType, int status)
        {
            FloodFreeContext db = new FloodFreeContext();
            List<Subscription> subcribers = db.Subscriptions
                .Where(x => x.SensorId.Equals(sensorId)
                && x.StatusType.Equals(statusType)).ToList();



            IUserMailer mailer = new UserMailer();

            foreach(Subscription subcriber in subcribers){
                if (statusType == 1 && status == 1)
                {
                    mailer.FloodedArea(subcriber.Username, subcriber.SensorId).SendAsync();
                }

                if (statusType == 1 && status == 0)
                {
                    mailer.UnfloodedArea(subcriber.Username, subcriber.SensorId).SendAsync();
                }
            }

        }

        private void NotifyByPhoneMessage(int sensorId, int statusType, int status)
        {
        }
    }


}
